package com.toobigtofail.entities
{
	import Box2D.Dynamics.b2Body;
	
	import com.toobigtofail.images.ShapeImage;
	import com.toobigtofail.shapes.IShape;
	
	import fabulaexmachina.box2fp.Box2DEntity;
	import fabulaexmachina.box2fp.graphics.SuperGraphiclist;
	
	public class BuildingBlock extends Box2DEntity
	{
		public static const TYPE:String = "building_block";
		
		private var _initOptions:Object;
		private var _shape:IShape;
		private var _image:ShapeImage;
		
		public function BuildingBlock(shape:IShape, angle:Number=0, image:ShapeImage=null)
		{
			_shape = shape;
			
			super(shape.X, shape.Y, shape.Width, shape.Height, b2Body.b2_dynamicBody);
			
			type = TYPE;
			_initOptions = {angle: angle};
			
			if (image) {
				_image = image;
				_image.type = "BuildingBlock";
				_image.angle = 0;
				_image.offsetForBox2D(Width, Height);
				graphic = new SuperGraphiclist(_image);
			}
		}
		
		override public function added():void {
			super.added();
			
			_shape.buildShape(body);
			angle = _initOptions.angle;
		}
		
		override public function update():void {
			super.update();
			
			if (_image) {
				_image.centerOrigin();
			}
		}
		
		public function get shape():IShape { return _shape; }
		public function get image():ShapeImage { return _image; }
	}
}